//	Some portions of this code are Copyright 1992  Thomas A. Dilligan
//		All Rights Reserved
//
// For best results, set tabstop=4

#import "SimpleCamera.h"

//This code Shamelessly pinched from Simple.app and then heavily modified.

@implementation SimpleCamera
- initFrame:(const NXRect *) theRect
{
  	RtPoint fromP = {0,0,5.0}, toP = {0,0,0};
	id aShape;
	[super initFrame:theRect];
	[self setEyeAt:fromP toward:toP roll:0.0];
	aShape=[[SimpleShape alloc] init];
	[[self setWorldShape:aShape] free];
	[self setSurfaceTypeForAll:N3D_SmoothSolids chooseHider:YES];
	theRotator=[[N3DRotator alloc] initWithCamera:self];
	return self;
}

- displayPhotoreal;
{
	char text[256];
	[self setDelegate:self];
	rendering=[self renderAsTIFF];
	if(rendering)
	{
		strcpy(origionalTitle,[window title]);
		sprintf(text,"%s - Rendering...",origionalTitle);
		[window setTitle:text];
	}
	return self;
}

- camera:theCamera didRenderStream:(NXStream *)imageStream  tag:(int)theJob frameNumber:(int)currentFrame
{
	id bitmap;
	char text[256],*ending;
	strcpy(text,[window title]);
	ending=rindex(text,'-');
	ending[-1]='\0';
	[window setTitle:text];
	bitmap=[[NXBitmapImageRep alloc] initFromStream:imageStream];
	[self lockFocus];
	[bitmap draw];
	[self unlockFocus];
	rendering=0;
	[window flushWindow];
	return self;
}


#define ACTIVEBUTTONMASK (NX_MOUSEUPMASK|NX_MOUSEDRAGGEDMASK)
- mouseDown:(NXEvent *)theEvent
{
	int                 	oldMask;
	NXPoint		oldMouse, newMouse, dMouse;
	RtMatrix		rmat, irmat;

	if(rendering)
		return self;
	[theRotator setRotationAxis:N3D_AllAxes];
	[self lockFocus];
	oldMask = [window addToEventMask:ACTIVEBUTTONMASK];
  
	[self setSurfaceTypeForAll:N3D_WireFrame chooseHider:YES];
  
	oldMouse = theEvent->location;
	[self convertPoint:&oldMouse fromView:nil];
	while (1)
	{
		newMouse = theEvent->location;
		[self convertPoint:&newMouse fromView:nil];
		dMouse.x = newMouse.x - oldMouse.x;
		dMouse.y = newMouse.y - oldMouse.y;
		if (dMouse.x != 0.0 || dMouse.y != 0.0)
		{
			[theRotator trackMouseFrom:&oldMouse to:&newMouse
			rotationMatrix:rmat andInverse:irmat];
			[worldShape concatTransformMatrix:rmat premultiply:NO];
			[self display];
		}
		theEvent = [NXApp getNextEvent:ACTIVEBUTTONMASK];
		if (theEvent->type == NX_MOUSEUP)
			break;
		oldMouse = newMouse;
  }
	[self setSurfaceTypeForAll:N3D_SmoothSolids chooseHider:YES];	
	[self display];
	[self unlockFocus];

	[window setEventMask:oldMask];
	return self;
}

@end
